package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.FindOrdersDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 查看历史(最新)订单 -- 前台
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R getOrders(Integer page,Integer pageSize){
        IPage<OrdersDto> pageInfo = orderService.getOrders(page,pageSize);
        return R.success(pageInfo);
    }

    /**
     * 订单查询 -- 后台
     * @param findOrdersDto -- 封装查询条件
     * @return
     */
    @GetMapping("/page")
    public R findByPage(FindOrdersDto findOrdersDto){
        IPage<OrdersDto> pageInfo = orderService.findByPage(findOrdersDto);
        return R.success(pageInfo);
    }

    /**
     * 后台订单派送
     * @param orders
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody Orders orders){
        orderService.updateStatus(orders);
        return R.success("操作成功");
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        orderService.again(orders);
        return R.success("操作成功");
    }

}